<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
          <el-form-item  label="上级分类" prop="parentId">
            <treeselect   style="width: 250px" v-model="queryParams.parentId" :options="treeData" :normalizer="normalizer" placeholder="请选择上级分类" />
          </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['category:category:add']"
        >添加分类</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleRemove"
          v-hasPermi="['category:category:add']"
        >物品迁移</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="categoryList" border  style="width: 100%">
      <el-table-column  label="序号" type="index" width="50"/>
      <el-table-column label="上级分类" align="center" prop="parentId" />
      <el-table-column label="分类" align="center" prop="categoryName" />
      <el-table-column label="描述" align="center" prop="categoryDesc" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['category:category:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['category:category:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改物品分类信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item>
          <input type="hidden" v-model="form.id"/>
        </el-form-item>

        <el-form-item label="上级分类" prop="categoryDesc">
          <treeselect  v-model="form.parentId" :options="treeData" :normalizer="normalizer" placeholder="选择上级分类" />
        </el-form-item>

        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="form.categoryName" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item  label="描述" prop="categoryDesc">
          <el-input type="textarea" v-model="form.categoryDesc" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer"  class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 物品迁移弹出框 -->
    <el-dialog title="物品迁移" :visible.sync="removeOpen" width="500px" append-to-body>
      <el-form ref="form" :model="removeForm" :rules="rules" label-width="100px">

        <el-form-item label="被迁移分类" prop="categoryDesc">
          <treeselect  v-model="removeForm.id1" :options="treeData" :normalizer="normalizer" placeholder="选择被迁移分类" />
        </el-form-item>
        <el-form-item label="迁移到" prop="categoryDesc">
          <treeselect  v-model="removeForm.id2" :options="treeData" :normalizer="normalizer" placeholder="选择迁移到" />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="removeForm.agree">包括子分类</el-checkbox>
        </el-form-item>
      </el-form>
      <div slot="footer"  class="dialog-footer">
        <el-button type="primary" @click="submitRemoveForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { remove,listCategory, getCategory, delCategory, addCategory, updateCategory,treeCategoryDate } from "@/api/goods/category";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Category",
  components: { Treeselect },
  data() {
    return {

      //分级树
      treeData:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物品分类信息表格数据
      categoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      removeOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryName: null,
        categoryDesc: null,
        busiPath: null,
        keyword:null,
        parentId:null,
      },
      // 表单参数
      form: {},
      removeForm: {
      },
      // 表单校验
      rules: {
        categoryName: [
          { required: true, message: "参数名称不能为空", trigger: "blur" },
          {max:30,message: "字符数不能超过30",trigger: "blur"}
        ],
        categoryDesc: [
          { required: false, message: "参数名称不能为空", trigger: "blur" },
          {max:200,message: "字符数不能超过200",trigger: "blur"}
        ],
      },
    };
  },

  created() {
    this.getList();
    this.treeList();
  },


  methods: {
    /** 搜索框树形图数据转换 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.categoryName,
        children: node.children
      };
    },
    /** 查询物品分类信息列表 */
    getList() {
      this.loading = true;
      listCategory(this.queryParams).then(response => {
        this.categoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询物品分类树形表 */
    treeList() {
      this.loading = true;
      treeCategoryDate().then(response => {
        this.treeData= response.data
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        categoryName: null,
        categoryDesc: null,
        busiPath: null,
        parentId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物品分类信息";
    },
    /*物品迁移按钮*/
    handleRemove(){
      this.removeForm ={
        id1:null,
        id2:null,
        agree: true
      }
      this.removeOpen = true;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
        const id = row.id
        getCategory(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改物品分类信息";
          this.getList();
          this.treeList();
        });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCategory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.treeList();
            });
          } else {
            addCategory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.treeList();
            });
          }
        }
      });
    },
    submitRemoveForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          remove(this.removeForm).then(response => {
            this.$modal.msgSuccess("迁移成功");
            this.removeOpen = false;
            this.getList();
            this.getList();
          });
        }
      })
    },


    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id ;
      this.$modal.confirm('是否确认删除物品分类信息编号为"' + id + '"的数据项？').then(function() {
        return delCategory(id);
      }).then(() => {
        this.getList();
        this.treeList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
